package com.tarena.elts.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * 登陆布局规划
 */
public class LoginFrame extends JFrame {
	private ClientContext clientContext; // 默认值null
	private JTextField idField;
	private JPasswordField pwdField;

	public int getUserId() {
		int id;
		id = Integer.parseInt(idField.getText());
		return id;
	}

	public String getPwd() {
		return new String(pwdField.getPassword());
	}

	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}

	/**
	 * 显示错误消息
	 * 
	 * @param msg
	 */
	private JLabel msgLabel;

	public void showMessage(String msg) {
		msgLabel.setForeground(Color.red);
		msgLabel.setText(msg);
		// 利用Timer实现显示5秒以后消失
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				msgLabel.setText("");
				timer.cancel();
			}
		}, 5000);
		// 当前窗口晃动一下！
		final int[] offset = { -1, -3, -8, -3, -1, 0, 1, 3, 8, 3, 1, 0 };
		final Timer timer2 = new Timer();
		final Point location = this.getLocation();
		timer2.schedule(new TimerTask() {
			int i = 0;

			@Override
			public void run() {
				// TODO Auto-generated method stub
				int x = location.x + offset[i++ % offset.length];
				int y = location.y;
				setLocation(x, y);
			}
		}, 0, 1000 / 10);
		timer2.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				setLocation(location);
				timer2.cancel();
			}
		}, 2000);
	}

	public LoginFrame() {
		init();
	}

	private void init() {
		this.setSize(300, 200);
		this.setLocationRelativeTo(null); // 居中
		this.add(createContentPane());
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				clientContext.exit(LoginFrame.this);
			}			
		});
	}

	private JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(BorderLayout.NORTH, new JLabel("登录", JLabel.CENTER));
		panel.add(BorderLayout.CENTER, createCenterPane());
		panel.add(BorderLayout.SOUTH, createToolsPane());
		return panel;
	}

	private JPanel createToolsPane() {
		JPanel panel = new JPanel();
		JButton login = new JButton("登录");
		JButton cancel = new JButton("取消");
		panel.add(login);
		panel.add(cancel);
		this.getRootPane().setDefaultButton(login); // 默认
		// 按键监听
		login.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// System.out.println("Login Button Click");
				// 如果值为空会出现空指针异常
				clientContext.login(); // 界面流程控制器
			}
		});
		cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.exit(LoginFrame.this);
			}
		});
		return panel;
	}

	private JPanel createCenterPane() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(6, 6, 6, 6));
		panel.add(BorderLayout.NORTH, createPwdIdPane());
		msgLabel = new JLabel("", JLabel.CENTER); //
		panel.add(BorderLayout.CENTER, msgLabel);
		return panel;
	}

	private JPanel createPwdIdPane() {
		JPanel panel = new JPanel(new GridLayout(2, 1, 0, 10));
		panel.add(createIdPane());
		panel.add(createPwdPane());
		return panel;
	}

	private JPanel createIdPane() {
		JPanel panel = new JPanel(new BorderLayout());
		JTextField idField = new JTextField();
		this.idField = idField; // 必须加
		JLabel idLabel = new JLabel("编号:");
		panel.add(BorderLayout.WEST, idLabel);
		panel.add(BorderLayout.CENTER, idField);
		return panel;
	}

	private JPanel createPwdPane() {
		JPanel panel = new JPanel(new BorderLayout());
		// JPasswordField pwdField = new JPasswordField();
		// this.pwdField=pwdField; 或下面
		pwdField = new JPasswordField();
		JLabel pwdlJLabel = new JLabel("密码:");
		panel.add(BorderLayout.WEST, pwdlJLabel);
		panel.add(BorderLayout.CENTER, pwdField);
		pwdField.enableInputMethods(true);
		return panel;
	}

}
